﻿Imports Newtonsoft.Json.Linq

Module modMain
    Public Declare Auto Function RegisterHotKey Lib "user32.dll" Alias "RegisterHotKey" (hwnd As IntPtr, id As Integer, fsModifiers As Integer, vk As Integer) As Boolean
    Public Declare Auto Function UnRegisterHotKey Lib "user32.dll" Alias "UnregisterHotKey" (hwnd As IntPtr, id As Integer) As Boolean

    Public Const WM_HOTKEY = 786
    Public Const MOD_NOREPEAT = 16384
    Public Const MOD_ALT = 1
    Public Const MOD_CONTROL = 2
    Public Const MOD_SHIFT = 4
    Public Const MOD_WIN = 8

    Public ConfigData As New JObject
    Public CmdList As New CommandList
    Public Themes As New ColorClass

    Public AppUpdateTime As String = "2020-12-26 14:00:00"
    Public AppUpdateUrl As String = "https://gitee.com/a101084/update/raw/master/SDRun/UpdateInfo.txt"
    Public AppUrl As String = "https://gitee.com/a101084/sdrun"
    Public AppName As String = "SDRun"
    Public AppDescription As String = "简洁轻量、容易使用的应用程序快速启动工具"
    Public AppVersion As String = "1.2.0"
    Public ConfigPath As String = Application.StartupPath + "\config.json"
    Public JsonInfo As String = "//如果你对这个文件的格式并不了解，请勿修改，以免造成程序启动错误。" + vbCrLf
    Public FontFamily As String = "微软雅黑"

    'Public Sub Main()
    '    Application.EnableVisualStyles()
    '    Application.Run(New FrmMain())
    'End Sub

    Public Function ListViewExists(Control As ListView, Title As String) As Boolean
        If Control.Items.Count > 0 Then
            Dim i As Integer
            For i = 0 To Control.Items.Count - 1
                If Title = Control.Items(i).Text Then
                    Return True
                End If
            Next
        End If
        Return False
    End Function

    Public Function RemoveAt(Of T)(arr As T(), index As Integer) As T() '删除数组指定元素
        Dim uBound = arr.GetUpperBound(0)
        Dim lBound = arr.GetLowerBound(0)
        Dim arrLen = uBound - lBound
        If index < lBound OrElse index > uBound Then
            Throw New ArgumentOutOfRangeException(String.Format("Index must be from {0} to {1}.", lBound, uBound))
        Else
            Dim outArr(arrLen - 1) As T
            Array.Copy(arr, 0, outArr, 0, index)
            Array.Copy(arr, index + 1, outArr, index, uBound - index)
            Return outArr
        End If
    End Function

    Public Function RemoveRpeat(Of T)(arr As T()) As T() '删除数组重复元素
        Dim i As Integer, j As Integer, k As Integer
        Dim arrT() As T = arr
        i = arrT.GetLowerBound(0)
        j = arrT.GetUpperBound(0)
        Do While i <= j
            For k = arrT.GetUpperBound(0) To i + 1 Step -1
                If arrT(i).Equals(arrT(k)) Then
                    arrT = RemoveAt(arrT, k)
                    j -= 1
                End If
            Next
            i += 1
        Loop
        Return arrT
    End Function

    Public Function RemoveNull(Of T)(arr As T()) As T() '删除数组空元素
        Dim i As Integer
        Dim arrT() As T = arr
        For i = arrT.GetUpperBound(0) To arrT.GetLowerBound(0) Step -1
            If IsNothing(arrT(i)) Or arrT(i).ToString = "" Then
                arrT = RemoveAt(arrT, i)
            End If
        Next
        Return arrT
    End Function

    Public Function LoadImage(Path As String) As Image
        Dim img As Image
        Try
            Dim s As IO.Stream = IO.File.Open(Path, IO.FileMode.Open)
            img = Image.FromStream(s)
            s.Close()
        Catch ex As Exception
            Throw New ArgumentException(ex.Message)
        End Try
        Return img
    End Function

    Public Function GetKey(strKey As String, Optional IsModifiers As Boolean = False) As Integer
        Dim key As Integer
        If IsModifiers Then
            If strKey = "ShiftKey" Then
                key = MOD_SHIFT
            ElseIf strKey = "ControlKey" Then
                key = MOD_CONTROL
            ElseIf strKey = "Menu" Then
                key = MOD_ALT
            ElseIf strKey = "LWin" Then
                key = MOD_WIN
            End If
        Else
            key = DirectCast([Enum].Parse(GetType(Keys), strKey), Keys)
        End If
        Return key
    End Function

    Public Function GetArgs(strCmd As String) As String
        Dim Args As String
        If strCmd.Substring(0, 1) = Chr(34) Then
            Args = strCmd.Substring(strCmd.IndexOf(Chr(34), 2) + 1)
        Else
            If strCmd.IndexOf(" ") > 0 Then
                Args = strCmd.Substring(strCmd.IndexOf(" ") + 1)
            Else
                Args = ""
            End If
        End If
        Return Args
    End Function

    Public Function GetCmd(strCmd As String, Optional IsCommand As Boolean = False) As String
        Dim s As String = ""
        If strCmd <> "" Then
            If strCmd.Substring(0, 1) = Chr(34) Then
                s = UpdateDLL.GetString(strCmd, Chr(34), Chr(34))
            Else
                If strCmd.IndexOf(" ") > 0 Then
                    Dim a() As String
                    a = strCmd.Split(" ")
                    s = a(0)
                Else
                    s = strCmd
                End If
            End If
            If IsCommand Then
                s = IO.Path.GetFileNameWithoutExtension(s)
            End If
        End If
        Return s
    End Function
End Module
